//Time1.java

// Time1 class definition

import java.text.DecimalFormat;  // used for number formatting

 

// This class maintains the time in 24-hour format

public class Time1  {

   private int hour;     // 0 - 23

   private int minute;   // 0 - 59

   private int second;   // 0 - 59

 

   // Time1 constructor initializes each instance variable

   // to zero. Ensures that each Time1 object starts in a

   // consistent state.

   public Time1()

   {

      setTime( 0, 0, 0 );

   }

 

   // Set a new time value using universal time. Perform

   // validity checks on the data. Set invalid values to zero.

   public void setTime( int h, int m, int s )

   {

      hour = ( ( h >= 0 && h < 24 ) ? h : 0 );

      minute = ( ( m >= 0 && m < 60 ) ? m : 0 );

      second = ( ( s >= 0 && s < 60 ) ? s : 0 );

   }

 

   // Convert to String in universal-time format

   public String toUniversalString()

   {

      DecimalFormat twoDigits = new DecimalFormat( "00" );

 

      return twoDigits.format( hour ) + ":" +

             twoDigits.format( minute ) + ":" +

             twoDigits.format( second );

   }

 

   // Convert to String in standard-time format

   public String toString()

   {

      DecimalFormat twoDigits = new DecimalFormat( "00" );

     

      return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) +

             ":" + twoDigits.format( minute ) +

             ":" + twoDigits.format( second ) +

             ( hour < 12 ? " AM" : " PM" );

   }

}

 

------------------------------------------------------------------------------------------------------------

//TimeTest.java

// Class TimeTest to exercise class Time1

import javax.swing.JOptionPane;

 

public class TimeTest {

   public static void main( String args[] )

   {

      Time1 t = new Time1();  // calls Time1 constructor

      String output;

 

      output = "The initial universal time is: " +

               t.toUniversalString() +

               "\nThe initial standard time is: " +

               t.toString() +

               "\nImplicit toString() call: " + t;

 

      t.setTime( 13, 27, 6 );

      output += "\n\nUniversal time after setTime is: " +

                t.toUniversalString() +

                "\nStandard time after setTime is: " +

                t.toString();

 

      t.setTime( 99, 99, 99 );  // all invalid values

      output += "\n\nAfter attempting invalid settings: " +

                "\nUniversal time: " + t.toUniversalString() +

                "\nStandard time: " + t.toString();

 

      JOptionPane.showMessageDialog( null, output,

         "Testing Class Time1",

         JOptionPane.INFORMATION_MESSAGE );

 

      System.exit( 0 );

   }

}

 

//Time1.java

// Time1 class definition

import java.text.DecimalFormat;  // used for number formatting

 

// This class maintains the time in 24-hour format

public class Time1  {

   private int hour;     // 0 - 23

   private int minute;   // 0 - 59

   private int second;   // 0 - 59

 

   // Time1 constructor initializes each instance variable

   // to zero. Ensures that each Time1 object starts in a

   // consistent state.

   public Time1()

   {

      setTime( 0, 0, 0 );

   }

 

   // Set a new time value using universal time. Perform

   // validity checks on the data. Set invalid values to zero.

   public void setTime( int h, int m, int s )

   {

      hour = ( ( h >= 0 && h < 24 ) ? h : 0 );

      minute = ( ( m >= 0 && m < 60 ) ? m : 0 );

      second = ( ( s >= 0 && s < 60 ) ? s : 0 );

   }

 

   // Convert to String in universal-time format

   public String toUniversalString()

   {

      DecimalFormat twoDigits = new DecimalFormat( "00" );

 

      return twoDigits.format( hour ) + ":" +

             twoDigits.format( minute ) + ":" +

             twoDigits.format( second );

   }

 

   // Convert to String in standard-time format

   public String toString()

   {

      DecimalFormat twoDigits = new DecimalFormat( "00" );

     

      return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) +

             ":" + twoDigits.format( minute ) +

             ":" + twoDigits.format( second ) +

             ( hour < 12 ? " AM" : " PM" );

   }

}

//TimeTest.java

// Demonstrate errors resulting from attempts

// to access private class members.

public class TimeTest {

   public static void main( String args[] )

   {

      Time1 t = new Time1();

 

      t.hour = 7;

   }

}

 

 

//Time1.java

// Time1 class definition

 

import java.text.DecimalFormat;  // used for number formatting

 

// This class maintains the time in 24-hour format

public class Time1  {

   private int hour;     // 0 - 23

   private int minute;   // 0 - 59

   private int second;   // 0 - 59

 

   // Time1 constructor initializes each instance variable

   // to zero. Ensures that each Time1 object starts in a

   // consistent state.

   public Time1()

   {

      setTime( 0, 0, 0 );

   }

 

   // Set a new time value using military time. Perform

   // validity checks on the data. Set invalid values

   // to zero.

   public void setTime( int h, int m, int s )

   {

      hour = ( ( h >= 0 && h < 24 ) ? h : 0 );

      minute = ( ( m >= 0 && m < 60 ) ? m : 0 );

      second = ( ( s >= 0 && s < 60 ) ? s : 0 );

   }

 

   // Convert to String in universal-time format

   public String toUniversalString()

   {

      DecimalFormat twoDigits = new DecimalFormat( "00" );

 

      return twoDigits.format( hour ) + ":" +

             twoDigits.format( minute ) + ":" +

             twoDigits.format( second );

   }

 

   // Convert to String in standard-time format

   public String toString()

   {

      DecimalFormat twoDigits = new DecimalFormat( "00" );

     

      return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) +

             ":" + twoDigits.format( minute ) +

             ":" + twoDigits.format( second ) +

             ( hour < 12 ? " AM" : " PM" );

   }

}

//TimeTest.java

 

import javax.swing.JOptionPane;

 

 

public class TimeTest {

   public static void main( String args[] )

   {  

      Time1 t = new Time1();

 

      t.setTime( 13, 27, 06 );

      String output =

         "Universal time is: " + t.toUniversalString() +

         "\nStandard time is: " + t.toString();

 

      JOptionPane.showMessageDialog( null, output,

         "Packaging Class Time1 for Reuse",

         JOptionPane.INFORMATION_MESSAGE );

 

      System.exit( 0 );

   }

}

 

//Time2.java

// Time2 class definition

 

import java.text.DecimalFormat;  // used for number formatting

 

// This class maintains the time in 24-hour format

public class Time2  {

   private int hour;     // 0 - 23

   private int minute;   // 0 - 59

   private int second;   // 0 - 59

 

   // Time2 constructor initializes each instance variable

   // to zero. Ensures that Time object starts in a

   // consistent state.

   public Time2() { setTime( 0, 0, 0 ); }

 

   // Time2 constructor: hour supplied, minute and second

   // defaulted to 0.

   public Time2( int h ) { setTime( h, 0, 0 ); }

 

   // Time2 constructor: hour and minute supplied, second

   // defaulted to 0.

   public Time2( int h, int m ) { setTime( h, m, 0 ); }

 

   // Time2 constructor: hour, minute and second supplied.

   public Time2( int h, int m, int s ) { setTime( h, m, s ); }

 

   // Time2 constructor: another Time2 object supplied.

   public Time2( Time2 time )

   {

      setTime( time.hour, time.minute, time.second );

   }

 

   // Set a new time value using universal time. Perform

   // validity checks on the data. Set invalid values to zero.

   public void setTime( int h, int m, int s )

   {

      hour = ( ( h >= 0 && h < 24 ) ? h : 0 );

      minute = ( ( m >= 0 && m < 60 ) ? m : 0 );

      second = ( ( s >= 0 && s < 60 ) ? s : 0 );

   }

 

   // Convert to String in universal-time format

   public String toUniversalString()

   {

      DecimalFormat twoDigits = new DecimalFormat( "00" );

 

      return twoDigits.format( hour ) + ":" +

             twoDigits.format( minute ) + ":" +

             twoDigits.format( second );

   }

 

   // Convert to String in standard-time format

   public String toString()

   {

      DecimalFormat twoDigits = new DecimalFormat( "00" );

     

      return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) +

             ":" + twoDigits.format( minute ) +

             ":" + twoDigits.format( second ) +

             ( hour < 12 ? " AM" : " PM" );

   }

}

//TimeTest.java

// Using overloaded constructors

import javax.swing.*;

 

public class TimeTest {

   public static void main( String args[] )

   {

      Time2 t1, t2, t3, t4, t5, t6;

      String output;

 

      t1 = new Time2();

      t2 = new Time2( 2 );

      t3 = new Time2( 21, 34 );

      t4 = new Time2( 12, 25, 42 );

      t5 = new Time2( 27, 74, 99 );

      t6 = new Time2( t4 );   // use t4 as initial value

 

      output = "Constructed with: " +

               "\nt1: all arguments defaulted" +

               "\n      " + t1.toUniversalString() +

               "\n      " + t1.toString();

 

      output += "\nt2: hour specified; minute and " +

                "second defaulted" +

                "\n      " + t2.toUniversalString() +

                "\n      " + t2.toString();

 

      output += "\nt3: hour and minute specified; " +

                "second defaulted" +

                "\n      " + t3.toUniversalString() +

                "\n      " + t3.toString();

 

      output += "\nt4: hour, minute, and second specified" +

                "\n      " + t4.toUniversalString() +

                "\n      " + t4.toString();

 

      output += "\nt5: all invalid values specified" +

                "\n      " + t5.toUniversalString() +

                "\n      " + t5.toString();

 

      output += "\nt6: Time2 object t4 specified" +

                "\n      " + t6.toUniversalString() +

                "\n      " + t6.toString();

 

      JOptionPane.showMessageDialog( null, output,

         "Demonstrating Overloaded Constructors",

         JOptionPane.INFORMATION_MESSAGE );

 

      System.exit( 0 );

   }

}

 

//Time3.java

// Time3 class definition

import java.text.DecimalFormat;  // used for number formatting

 

// This class maintains the time in 24-hour format

public class Time3 {

   private int hour;     // 0 - 23

   private int minute;   // 0 - 59

   private int second;   // 0 - 59

 

   // Time3 constructor initializes each instance variable

   // to zero. Ensures that Time object starts in a

   // consistent state.

   public Time3() { setTime( 0, 0, 0 ); }

 

   // Time3 constructor: hour supplied, minute and second

   // defaulted to 0.

   public Time3( int h ) { setTime( h, 0, 0 ); }

 

   // Time3 constructor: hour and minute supplied, second

   // defaulted to 0.

   public Time3( int h, int m ) { setTime( h, m, 0 ); }

 

   // Time3 constructor: hour, minute and second supplied.

   public Time3( int h, int m, int s ) { setTime( h, m, s ); }

 

   // Time3 constructor: another Time3 object supplied.

   public Time3( Time3 time )

   {

      setTime( time.getHour(),

               time.getMinute(),

               time.getSecond() );

   }

 

   // Set Methods

   // Set a new time value using universal time. Perform

   // validity checks on the data. Set invalid values to zero.

   public void setTime( int h, int m, int s )

   {

      setHour( h );    // set the hour

      setMinute( m );  // set the minute

      setSecond( s );  // set the second

   }

 

   // set the hour

   public void setHour( int h )

      { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); }

 

   // set the minute

   public void setMinute( int m )

      { minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); }

 

   // set the second

   public void setSecond( int s )

      { second = ( ( s >= 0 && s < 60 ) ? s : 0 ); }

 

   // Get Methods

   // get the hour

   public int getHour() { return hour; }

 

   // get the minute

   public int getMinute() { return minute; }

 

   // get the second

   public int getSecond() { return second; }

 

   // Convert to String in universal-time format

   public String toUniversalString()

   {

      DecimalFormat twoDigits = new DecimalFormat( "00" );

 

      return twoDigits.format( getHour() ) + ":" +

             twoDigits.format( getMinute() ) + ":" +

             twoDigits.format( getSecond() );

   }

 

   // Convert to String in standard-time format

   public String toString()

   {

      DecimalFormat twoDigits = new DecimalFormat( "00" );

 

      return ( ( getHour() == 12 || getHour() == 0 ) ?

               12 : getHour() % 12 ) + ":" +

             twoDigits.format( getMinute() ) + ":" +

             twoDigits.format( getSecond() ) +

             ( getHour() < 12 ? " AM" : " PM" );

   }

}

TimeTest.java

// Demonstrating the Time3 class set and get methods

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

 

public class TimeTest extends JApplet

                      implements ActionListener {

   private Time3 t;

   private JLabel hourLabel, minuteLabel, secondLabel;

   private JTextField hourField, minuteField,

                      secondField, display;

   private JButton tickButton;

 

   public void init()

   {

      t = new Time3();

 

      Container c = getContentPane();

 

      c.setLayout( new FlowLayout() );

      hourLabel = new JLabel( "Set Hour" );

      hourField = new JTextField( 10 );

      hourField.addActionListener( this );

      c.add( hourLabel );

      c.add( hourField );

 

      minuteLabel = new JLabel( "Set minute" );

      minuteField = new JTextField( 10 );

      minuteField.addActionListener( this );

      c.add( minuteLabel );

      c.add( minuteField );

 

      secondLabel = new JLabel( "Set Second" );

      secondField = new JTextField( 10 );

      secondField.addActionListener( this );

      c.add( secondLabel );

      c.add( secondField );

 

      display = new JTextField( 30 );

      display.setEditable( false );

      c.add( display );

 

      tickButton = new JButton( "Add 1 to Second" );

      tickButton.addActionListener( this );

      c.add( tickButton );

 

      updateDisplay();      

   }

 

   public void actionPerformed( ActionEvent e )

   {

      if ( e.getSource() == tickButton )

         tick();

      else if ( e.getSource() == hourField ) {

         t.setHour(

            Integer.parseInt( e.getActionCommand() ) );

         hourField.setText( "" );

      }

      else if ( e.getSource() == minuteField ) {

         t.setMinute(

            Integer.parseInt( e.getActionCommand() ) );

         minuteField.setText( "" );

      }

      else if ( e.getSource() == secondField ) {

         t.setSecond(

            Integer.parseInt( e.getActionCommand() ) );

         secondField.setText( "" );

      }

 

      updateDisplay();

   }

 

   public void updateDisplay()

   {

      display.setText( "Hour: " + t.getHour() +

         "; Minute: " + t.getMinute() +

         "; Second: " + t.getSecond() );

      showStatus( "Standard time is: " + t.toString() +

         "; Universal time is: " + t.toUniversalString() );

   }

 

   public void tick()

   {

      t.setSecond( ( t.getSecond() + 1 ) % 60 );

 

      if ( t.getSecond() == 0 ) {

         t.setMinute( ( t.getMinute() + 1 ) % 60 );

 

         if ( t.getMinute() == 0 )

            t.setHour( ( t.getHour() + 1 ) % 24 );

      }

   }

}

 

 

//Increment.java

// Initializing a final variable

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class Increment extends JApplet

             implements ActionListener {

   private int count = 0, total = 0;

   private final int INCREMENT = 5; // constant variable

 

   private JButton incr; 

 

   public void init()

   {

      Container c = getContentPane();

 

      incr = new JButton( "Click to increment" );

      incr.addActionListener( this );

      c.add( incr );

   }

 

   public void actionPerformed( ActionEvent e )

   {

      total += INCREMENT;

      count++;

      showStatus( "After increment " + count +

                  ": total = " + total );

   }

}